package com.co.persistence;

import java.util.ArrayList;
import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.co.model.bean.User;
import com.co.utils.Logger;

public class PersistenceManager {

	private static PersistenceManager manager;

	public static PersistenceManager getInstance() {
		if (manager == null) {
			manager = new PersistenceManager();
		}
		return manager;
	}

	public Session getSession() {
		Session session = null;

		try {
			SessionFactory sessionFactory = null;
			Configuration conf = new Configuration().configure();

			sessionFactory = conf.buildSessionFactory();
			session = sessionFactory.openSession();

		} catch (Exception e) {
			Logger.showMessage("Error in creating SessionFactory object.");
			Logger.showMessage(e.getMessage());
			throw new ExceptionInInitializerError(e);
		} finally {
			session.flush();
			session.close();
		}
		return session;
	}

	public Transaction beginTransaction(Session session) {
		Transaction tx = session.beginTransaction();
		return tx;
	}

	public void confirmTransaction(Transaction tx) {
		tx.commit();
	}

	public ArrayList<User> selectUsers(Session session, String strQuery) {
		ArrayList<User> users = new ArrayList<User>();
		users.clear();

		Logger.showMessage("Selecting Record");

		// Query test = session.createSQLQuery("CALL GET_CURRENT_DATE()");

		Query query = session.createQuery(strQuery);
		Iterator it = query.iterate();

		while (it.hasNext()) {
			User user = (User) it.next();
			Logger.showMessage(user.getInfo());
			users.add(user);
		}

		return users;
	}

	//
	// public Collection<Customer> getAllCustomers(Session session) {
	// String strQuery = "from Customer c";
	//
	// Query query = session.createQuery(strQuery);
	// Iterator it = query.iterate();
	//
	// Collection<Customer> customers = new ArrayList<Customer>();
	// Customer newCustomer = null;
	//
	// while (it.hasNext()) {
	// Customer c = (Customer) it.next();
	// newCustomer = new Customer();
	//
	// newCustomer.setDocumentNumber(c.getDocumentNumber());
	// newCustomer.setDocumentTypeId(c.getDocumentTypeId());
	// newCustomer.setName(c.getName());
	// newCustomer.setSurname(c.getSurname());
	// newCustomer.setBirthDate(c.getBirthDate());
	// newCustomer.setAddress(c.getAddress());
	// newCustomer.setCity(c.getCity());
	// newCustomer.setState(c.getState());
	// newCustomer.setStateCode(c.getStateCode());
	// newCustomer.setPhone(c.getPhone());
	// newCustomer.setPersonalPhone(c.getPersonalPhone());
	// newCustomer.setStatus(c.getStatus());
	// System.out.println("------------");
	//
	// customers.add(newCustomer);
	// }
	// return customers;
	// }
	//
	// public Collection<Asunto> getAllAsuntos(Session session) {
	// String strQuery = "from Asunto a";
	//
	// Query query = session.createQuery(strQuery);
	// Iterator it = query.iterate();
	//
	// Collection<Asunto> asuntos = new ArrayList<Asunto>();
	// Asunto newAsunto = null;
	//
	// while (it.hasNext()) {
	// Asunto a = (Asunto) it.next();
	// newAsunto = new Asunto();
	//
	// newAsunto.setId(a.getId());
	// newAsunto.setDescription(a.getDescription());
	// newAsunto.setCreationDate(a.getCreationDate());
	// newAsunto.setStatus(a.getStatus());
	// System.out.println("------------");
	//
	// asuntos.add(newAsunto);
	// }
	// return asuntos;
	// }

	public void doAll(String username, String password) {

		Session session = null;
		try {
			session = getSession();
			Transaction tx = beginTransaction(session);

			System.out.println("Done");

		} catch (Throwable e) {

			System.err.println("Error in creating SessionFactory object."
					+ e.getMessage());
			throw new ExceptionInInitializerError(e);

		} finally {
			session.flush();
			session.close();

		}
	}

	public Session connect() {
		Session session = null;
		try {
			HibernateUtils u = new HibernateUtils();
			session = u.getSessionFactory().openSession();
		} catch (Exception e) {
		}
		return session;
	}

	public void releaseConnection() {

	}
}
